package com.pic.mgt.monitor.parser.syushin;

import com.pic.mgt.monitor.WrongUnitFormatException;
import com.pic.mgt.monitor.parser.FormatParser;

public final class SyushinMessageTypeParser extends FormatParser {
	private int realMessageFilenameLen;
	private int bucketMessageFilenameLen;
	private String messageTransOkConfirmationFilenameSuffix;

	@Override
	public Object parse(Object _confirmationFilename) throws WrongUnitFormatException {
		// 確認是否為 String 型態
		// throws WrongUnitFormatException
		super.checkObjectType(_confirmationFilename, String.class);
		
		String confirmationFilename = (String)_confirmationFilename;
		
		// 取得該 confirmation file 的確認結尾字串
		String messageConfirmationFilenameSuffix;
		
		try {
			// 假設是 real message
			// throws StringIndexOutOfBoundsException
			messageConfirmationFilenameSuffix = confirmationFilename.substring(
															this.realMessageFilenameLen,
															confirmationFilename.length());
			if (messageConfirmationFilenameSuffix.equals(messageTransOkConfirmationFilenameSuffix)) {
				return messageConfirmationFilenameSuffix;
			}
		} catch (StringIndexOutOfBoundsException e) {
			// 表示不是 real 的 message
		}
		
		
		try {
			// 假設是 bucket message
			// throws StringIndexOutOfBoundsException
			messageConfirmationFilenameSuffix = confirmationFilename.substring(
															this.bucketMessageFilenameLen,
															confirmationFilename.length());
			if (messageConfirmationFilenameSuffix.equals(messageTransOkConfirmationFilenameSuffix)) {
				return messageConfirmationFilenameSuffix;
			}
		} catch (IndexOutOfBoundsException e) {
			// 表示不是 bucket 的 message
		}
		
		// 如果都不是表示檔案格式錯誤
		throw new WrongUnitFormatException(
				"Confirmation filename '" + confirmationFilename + "' format is invalid.");
	}
	
	// --- getter and setter ---

	public void setRealMessageFilenameLen(int realMessageFilenameLen) {
		this.realMessageFilenameLen = realMessageFilenameLen;
	}

	public void setBucketMessageFilenameLen(int bucketMessageFilenameLen) {
		this.bucketMessageFilenameLen = bucketMessageFilenameLen;
	}

	public void setMessageTransOkConfirmationFilenameSuffix(
			String messageTransOkConfirmationFilenameSuffix) {
		this.messageTransOkConfirmationFilenameSuffix = messageTransOkConfirmationFilenameSuffix;
	}

}
